package com.demo.villageresidence.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.villageresidence.dto.Result;
import com.demo.villageresidence.pojo.Activity;
import com.demo.villageresidence.pojo.Bidding;
import com.demo.villageresidence.pojo.Registration;
import com.demo.villageresidence.pojo.User;
import com.demo.villageresidence.service.ActivityService;
import com.demo.villageresidence.service.RegistrationService;
import com.demo.villageresidence.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/registration")
public class RegistrationController {

    @Autowired
    private RegistrationService registrationService;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private UserService userService;

    // 根据用户id查询报名过的活动
    @GetMapping("getRegistrationByUserId/{userId}")
    public Result getRegistrationByUserId(@PathVariable("userId") Integer userId){

        List<Registration> registrationList = registrationService.query().eq("user_id", userId).eq("activity_type",1).list();
        if (registrationList == null || registrationList.size() == 0){
            return Result.fail("没有报名过活动");
        }else {
            registrationList.forEach(
                    registration -> registration.setActivity(activityService.getById(registration.getActivityId()))
            );
            return Result.ok(registrationList);
        }
    }


    @GetMapping("getVolunteerRegistrationByUserId/{userId}")
    public Result getVolunteerRegistrationByUserId(@PathVariable("userId") Integer userId){

        List<Registration> registrationList = registrationService.query().eq("user_id", userId).eq("activity_type",0).list();
        if (registrationList == null || registrationList.size() == 0){
            return Result.fail("没有报名过志愿者活动");
        }else {
            registrationList.forEach(
                    registration -> registration.setActivity(activityService.getById(registration.getActivityId()))
            );
            return Result.ok(registrationList);
        }
    }

    @GetMapping("getRegistrationByRegistrationId/{registrationId}")
    public Result getRegistrationByRegistrationId(@PathVariable("registrationId") Integer registrationId){
        Registration registration = registrationService.getById(registrationId);
        Activity activity = activityService.getById(registration.getActivityId());
        registration.setActivity(activity);
        return Result.ok(registration);
    }



    // 报名活动
    @PostMapping("addRegistration")
    public Result addRegistration(@RequestBody Registration registration){
        if (registrationService.query().eq("user_id", registration.getUserId())
                .eq("activity_id", registration.getActivityId()).list().size() != 0){
            return Result.fail("已经报名过该活动");
        }else {
            if (activityService.getById(registration.getActivityId()).getActivityDeadline().before(new Date())) {
                return Result.fail("报名时间已过");
            }
            // 判断报名人数是否已满 活动最大人数activity_human
            List<Registration> activityId = registrationService.query().eq("activity_id", registration.getActivityId()).list();
            if (activityId.size() >= activityService.getById(registration.getActivityId()).getActivityHuman()){
                return Result.fail("报名人数已满");
            }
            registration.setRegistrationTime(LocalDateTime.now());
            registration.setIsDelete(0);

            if (registrationService.save(registration)){
                return Result.ok("报名成功");
            }else {
                return Result.fail("报名失败");
            }
        }
    }

    /**
     * 通过活动id查询报名的用户
     * @return
     */
    @GetMapping("getRegistrationByActivityId")
    public Result getRegistrationByActivityId(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                              @RequestParam(value = "activityId") Integer activityId){
        Page<Registration> registrationPage = registrationService.query().eq("activity_id", activityId).page(new Page<>(current, pageSize));
        List<Registration> registrationList = registrationPage.getRecords();
        long total = registrationPage.getTotal();
        List<User> userList = new ArrayList<>();
        for (Registration registration : registrationList) {
            Integer userId = registration.getUserId();
            User user = userService.getById(userId);
            userList.add(user);
        }
        if (userList == null || userList.size() == 0)
            return Result.fail("未有人报名该活动");
        else
            return Result.ok(userList,total);
    }

    @GetMapping("getRegistrationByDate")
    public Result getRegistrationByDate() {

        Integer registrationTime = registrationService.query().ge("registration_time", LocalDateTime.now().toLocalDate()).count();
        System.out.println(registrationTime);
        return Result.ok(registrationTime);

    }




}
